﻿using System;
using System.Web;
using System.Linq;
/// <summary>
/// Summary description for GioHang
/// </summary>
public class GioHang
{
    public static string GetAllProductsID()
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return "";
        string[] b = a.Split(',');
        string c = "";
        for (int i = 0; i < b.Length; i += 2)
            c += b[i] + ",";
        if (c != "" || c[c.Length - 1] == ',')
            c = c.Remove(c.Length - 1);
        return c;
    }

    public static int GetQuantity(int id)
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return 0;
        string[] b = a.Split(',');
        for (int i = 0; i < b.Length; i += 2)
            if (int.Parse(b[i]) == id)
                return Convert.ToInt32(b[i + 1]);
        return 0;
    }
    public static int countProducts()
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        string[] b = a.Split(',');
        return b.Length / 2;
    }

    public static int countTotal()
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return 0;
        string[] b = a.Split(',');
        int t = 0;
        for (int i = 1; i < b.Length; i += 2)
            t += int.Parse(b[i]);
        return t;
    }

    public static void delProduct(int id)
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return;
        string[] b = a.Split(',');
        string c = "";
        for (int i = 0; i < b.Length; i += 2)
        {
            if (int.Parse(b[i]) != id)
                c += b[i] + "," + b[i + 1] + ",";
        }
        if (c != "")
            if (c[c.Length - 1] == ',')
                c = c.Remove(c.Length - 1);
        HttpContext.Current.Session["giohang"] = c;
    }

    public static void delProducts(int[] id)
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return;
        string[] b = a.Split(',');
        string c = "";
        for (int i = 0; i < b.Length; i += 2)
        {
            if (!id.Contains(int.Parse(b[i])))
                c += b[i] + "," + b[i + 1] + ",";
        }
        if (c != "" || c[c.Length - 1] == ',')
            c = c.Remove(c.Length - 1);
        HttpContext.Current.Session["giohang"] = c;
    }

    public static void delCart()
    {
        HttpContext.Current.Session["giohang"] = "";
    }

    public static void updateCart(int id, int qtt)
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return;
        string[] b = a.Split(',');
        for (int i = 0; i < b.Length; i += 2)
        {
            if (int.Parse(b[i]) == id)
            {
                int t = int.Parse(b[i + 1]) + qtt;
                b[i + 1] = t.ToString();
                break;
            }
        }
        HttpContext.Current.Session["giohang"] = String.Join(",", b);
    }

    public static void changeCart(int id, int qtt)
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        if (a == "")
            return;
        string[] b = a.Split(',');
        for (int i = 0; i < b.Length; i += 2)
        {
            if (int.Parse(b[i]) == id)
            {
                b[i + 1] = qtt.ToString();
                break;
            }
        }
        HttpContext.Current.Session["giohang"] = String.Join(",", b);
    }

    public static void addCart(int id)
    {
        string a = (string)HttpContext.Current.Session["giohang"];
        bool k = false;

        if (a == "")
        {
            HttpContext.Current.Session["giohang"] = id.ToString() + ",1";
            return;
        }

        if (a.Length > 0)
        {
            string[] b = a.Split(',');

            for (int i = 0; i < b.Length; i += 2)
            {
                if (int.Parse(b[i]) == id)
                {
                    k = true;
                    break;
                }
            }
        }

        if (!k)
        {
            a += "," + id.ToString() + ",1";
            HttpContext.Current.Session["giohang"] = a;
        }
        else
            GioHang.updateCart(id, 1);
    }
}
